package com.whl.mybatis.config;

import com.whl.mybatis.builder.ConfigurationAfterBuilder;
import com.whl.mybatis.interceptors.PageInterceptor;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@org.springframework.context.annotation.Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
@MapperScan(MybatisConfiguration.mapperPackage)
public class MybatisConfiguration {
	protected static final String mapperPackage = "com.whl.project.mapper";
	private static final Logger logger = LoggerFactory.getLogger(MybatisConfiguration.class);

	@Autowired
	private SqlSession sqlSession;

	@PostConstruct
	public void afterPropertiesSet() {
		Configuration configuration = sqlSession.getConfiguration();
		PageInterceptor pageInterceptor = new PageInterceptor();
		configuration.addInterceptor(pageInterceptor);
		List<SqlCommandType> sqlCommandTypes = new ArrayList<>();
		sqlCommandTypes.add(SqlCommandType.INSERT);
		sqlCommandTypes.add(SqlCommandType.DELETE);
		sqlCommandTypes.add(SqlCommandType.UPDATE);
		sqlCommandTypes.add(SqlCommandType.SELECT);
		ConfigurationAfterBuilder.afterPropertiesSet(configuration, configuration.getLanguageRegistry().getDefaultDriver(), sqlCommandTypes);
		logger.debug("mapperPackage is ", mapperPackage);
	}

}
